package com.ljfiction.book.activitys;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ljfiction.book.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MothActivity extends BaseActivity {

    @BindView(R.id.back_mothactivity)
    ImageView backMothactivity;
    @BindView(R.id.year_recycle)
    RecyclerView yearRecycle;
    @BindView(R.id.toleft)
    ImageView toleft;
    @BindView(R.id.tolreight)
    ImageView tolreight;
    @BindView(R.id.moth_recycle)
    RecyclerView mothRecycle;

    private BaseQuickAdapter adpter, mothAdapter;
    private List<Integer> listyear = new ArrayList<>();
    private int Mothposition = 200;
    private int yearposition = 0;
    private List<Integer> listmoth = new ArrayList<>();
    private Intent intent;
    private int maxyear,maxmoth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_moth);
        ButterKnife.bind(this);
        intent = new Intent();
        maxyear=getIntent().getIntExtra("maxyear",2017);
        maxmoth=getIntent().getIntExtra("maxmoth",12);
        intView();
        intData();
    }

    private void intData() {
        for (int i = 1; i < 13; i++) {
            listmoth.add(i);
        }
        for (int i = 2018; i < maxyear+1; i++) {
            listyear.add(i);
        }
        yearposition = listyear.size() - 1;
        yearRecycle.scrollToPosition(yearposition);
        adpter.notifyDataSetChanged();
    }

    private void intView() {
        LinearLayoutManager la = new LinearLayoutManager(this);
        la.setOrientation(LinearLayoutManager.HORIZONTAL);
        yearRecycle.setLayoutManager(la);
        adpter = new BaseQuickAdapter<Integer, BaseViewHolder>(R.layout.year_item, listyear) {
            @Override
            protected void convert(BaseViewHolder helper, Integer item) {
                helper.setText(R.id.yeartext, String.valueOf(item));
            }
        };
        yearRecycle.setAdapter(adpter);
        mothRecycle.setLayoutManager(new GridLayoutManager(this, 4));
        mothRecycle.setAdapter(mothAdapter = new BaseQuickAdapter<Integer, BaseViewHolder>(R.layout.moth_item, listmoth) {
            @Override
            protected void convert(BaseViewHolder helper, Integer item) {
                if (item>maxmoth&&maxyear==listyear.get(yearposition)){
                    helper.setTextColor(R.id.mothtext, Color.parseColor("#999999"));
                }
                if (helper.getLayoutPosition() == Mothposition) {
                    helper.getView(R.id.mothtext).setBackgroundResource(R.drawable.shapyuanred);
                    helper.setTextColor(R.id.mothtext, Color.parseColor("#ffffff"));
                }
                helper.setText(R.id.mothtext, String.valueOf(item));
            }
        });
        mothAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (listmoth.get(position)>maxmoth&&maxyear==listyear.get(yearposition)){
                    showToast("这月的月报还没出哦!");
                    return;
                }
                Mothposition = position;
                mothAdapter.notifyDataSetChanged();
                intent.putExtra("year", listyear.get(yearposition));
                intent.putExtra("moth", listmoth.get(position));
                setResult(2, intent);
                goBack();
            }
        });
    }

    @OnClick({R.id.back_mothactivity, R.id.toleft, R.id.tolreight})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_mothactivity:
                goBack();
                break;
            case R.id.toleft:
                if (yearposition <= 0) {
                    showToast("上年还没有月报哦！");
                } else {
                    yearposition = yearposition - 1;
                    yearRecycle.scrollToPosition(yearposition);
                    if (yearposition <= 0) {
                        toleft.setImageResource(R.mipmap.lowno);
                    }
                    if (yearposition < listyear.size() - 1) {
                        tolreight.setImageResource(R.mipmap.front);
                    }
                }
                mothAdapter.notifyDataSetChanged();
                break;
            case R.id.tolreight:
                if (yearposition >= listyear.size() - 1) {
                    showToast("明年月报还没出哦！");

                } else {
                    yearposition = yearposition + 1;
                    yearRecycle.scrollToPosition(yearposition);
                    if (yearposition >= listyear.size() - 1) {
                        tolreight.setImageResource(R.mipmap.frontno);
                    }
                    if (yearposition > 0) {
                        toleft.setImageResource(R.mipmap.low);
                    }
                }
                mothAdapter.notifyDataSetChanged();
                break;
        }
    }
}
